package account

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"gorm.io/gorm"
	"insighttracking/query/dao"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type ModifyAccountStatusByIdApiLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewModifyAccountStatusByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyAccountStatusByIdApiLogic {
	hostname, _ := os.Hostname()
	return &ModifyAccountStatusByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ModifyAccountStatusByIdApiLogic) ModifyAccountStatusByIdApi(req *types.IdReq) (resp string, err error) {
	accountEntity, err := dao.AccountEntity.WithContext(l.ctx).Where(dao.AccountEntity.AccountID.Eq(req.Id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return "", errors.New("传递的id错误")
	}
	status := k.If(accountEntity.Status == 1, 2, 1)
	if _, err = dao.AccountEntity.WithContext(l.ctx).Where(dao.AccountEntity.AccountID.Eq(req.Id)).UpdateSimple(
		dao.AccountEntity.Status.Value(int64(status)),
	); err != nil {
		return "", errors.New("更新失败")
	}
	return
}
